package org.utils;

import com.google.common.collect.Maps;
import com.sojoys.artifact.tools.ToolDate;
import com.xiaoleilu.hutool.date.DateBetween;
import com.xiaoleilu.hutool.date.DateUnit;
import com.xiaoleilu.hutool.date.DateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by fxf on 2017/9/15.
 */
public class TimeUtil {

    public static Map<Integer,DateUnit> dateUnitMap = Maps.newHashMap();

    public static int MS = 1;
    public static int SECOND = 2;
    public static int MINUTE = 3;
    public static int HOUR = 4;
    public static int DAY = 5;
    public static int WEEK = 6;

    static {
        dateUnitMap.put(MS,DateUnit.MS);
        dateUnitMap.put(SECOND,DateUnit.SECOND);
        dateUnitMap.put(MINUTE,DateUnit.MINUTE);
        dateUnitMap.put(HOUR,DateUnit.HOUR);
        dateUnitMap.put(DAY,DateUnit.DAY);
        dateUnitMap.put(WEEK,DateUnit.WEEK);
    }

    public static DateUnit getDateUnit(int type){
        return dateUnitMap.get(type);
    }

    /**
     * 计算间隔时间
     * @param betweenTime 间隔时间
     * @param calendarUnit 间隔时间的时间单位：Calendar 常量
     * @param dateUnit hutool时间单位
     * @return
     */
    public static int getBetweenTime(int betweenTime ,int calendarUnit, DateUnit dateUnit){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c2.add(calendarUnit, betweenTime);
        DateBetween between = new DateBetween(c1.getTime(),c2.getTime(),true);
        return (int) between.between(dateUnit);
    }

    /**
     * 获取从今天0点到当前时间经过的毫秒数
     * @return
     */
    public static long getTodayTimeMillis(){
        Calendar zero = DateUtil.beginOfDay(Calendar.getInstance());
        return System.currentTimeMillis() - zero.getTimeInMillis();
    }

    /**
     * 获取从今天0点到当前时间经过的秒数
     * @return
     */
    public static int getTodayTimeSecond(){
        Calendar zero = DateUtil.beginOfDay(Calendar.getInstance());
        return (int) ((System.currentTimeMillis() - zero.getTimeInMillis())/1000);
    }

    /**
     * 获取从今天0点到当前时间经过的分钟数
     * @return
     */
    public static int getTodayTimeMinute(){
        Calendar zero = DateUtil.beginOfDay(Calendar.getInstance());
        return (int) ((System.currentTimeMillis() - zero.getTimeInMillis())/1000/60);
    }

    /**
     * 获取当前时间到第二天的毫秒数
     */
    public static long getToTomorrowTimeMillis(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR,1);
        Calendar zero = DateUtil.beginOfDay(c);
        return zero.getTimeInMillis() - System.currentTimeMillis();
    }

    /**
     * 获取当前时间到第二天的秒数
     */
    public static int getToTomorrowSecond(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR,1);
        Calendar zero = DateUtil.beginOfDay(c);
        return (int) ((zero.getTimeInMillis() - System.currentTimeMillis())/1000);
    }

    /**
     * 获得明天0点整时间
     */
    public static Date getTomorrowZeroTime(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR,1);
        Calendar zero = DateUtil.beginOfDay(c);
        return zero.getTime();
    }

    /**
     * 判断给定的两个毫秒数是否在同一天
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(long time1, long time2){
        Date date1 = new Date(time1);
        Date date2 = new Date(time2);
        return ToolDate.isSameDay(date1, date2);
    }

    /**
     * 获取从当天00:00:00到指定时间间隔分钟数
     * @param hour
     * @param minute
     * @return
     */
    public static int getBetweenMinuteFromBeginOfDay(int hour, int minute){
        Calendar zero = DateUtil.beginOfDay(Calendar.getInstance());
        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_YEAR,zero.get(Calendar.DAY_OF_YEAR));
        date.set(Calendar.HOUR_OF_DAY,hour);
        date.set(Calendar.MINUTE,minute);
        int result = (int) DateUtil.between(DateUtil.beginOfDay(zero).getTime(), date.getTime(), DateUnit.MINUTE);
        return result;
    }

    /**
     * 取calendar对象
     * @param hour 小时
     * @param minute 分钟
     */
    public static Calendar getCalendar(int hour,int minute){
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR_OF_DAY,hour);
        date.set(Calendar.MINUTE,minute);
        return date;
    }


    /**
     * 取calendar对象
     * @param hour 小时
     * @param minute 分钟
     */
    public static Calendar getCalendar(Date date,int hour,int minute){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date.getTime());
        c.set(Calendar.HOUR_OF_DAY,hour);
        c.set(Calendar.MINUTE,minute);
        c.set(Calendar.MILLISECOND,0);
        return c;
    }


    /**
     * 取calendar对象
     * @param dayOfYear 一年中的第几天
     * @param hour 小时
     * @param minute 分钟
     */
    public static Calendar getCalendarByDayOfYear(int dayOfYear,int hour,int minute){
        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_YEAR,dayOfYear);
        date.set(Calendar.DAY_OF_YEAR,hour);
        date.set(Calendar.MINUTE,minute);
        return date;
    }

    /**
     * 取calendar对象
     * @param dayOfMonth 一月中的第几天
     * @param hour 小时
     * @param minute 分钟
     */
    public static Calendar getCalendarByDayOfMonth(int dayOfMonth,int hour,int minute){
        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_MONTH,dayOfMonth);
        date.set(Calendar.DAY_OF_YEAR,hour);
        date.set(Calendar.MINUTE,minute);
        return date;
    }

    /**
     * 获取昨天是周几
     */
    public static int getYesterdayOfWeek(int today){
        if (today == Calendar.SUNDAY){
            return Calendar.SATURDAY;
        }
        return today - 1;
    }

    /**
     * 获取明天是周几
     */
    public static int getTomorrowOfWeek(int today){
        if (today == Calendar.SATURDAY){
            return Calendar.SUNDAY;
        }
        return today + 1;
    }

    /**
     * 获取昨天是几号
     */
    public static int getYesterdayOfMonth(int today){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,today);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)-1);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static Calendar getAssignCalendar(int[]time){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR,time[0]);
        c.set(Calendar.MONTH,time[1]);
        c.set(Calendar.DAY_OF_MONTH,time[2]);
        if (time.length > 3){
            c.set(Calendar.HOUR_OF_DAY,time[3]);
            c.set(Calendar.MINUTE,time[4]);
            c.set(Calendar.SECOND,time[5]);
        }else{
            c.set(Calendar.HOUR_OF_DAY,0);
            c.set(Calendar.MINUTE,0);
            c.set(Calendar.SECOND,0);
        }
        c.set(Calendar.MILLISECOND,0);
        return c;
    }

    public static Date getAssignDate(int[]time){
        return getAssignCalendar(time).getTime();
    }

    public static Calendar getCalendarByHourMinute(int hour,int minute){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,hour);
        c.set(Calendar.MINUTE,minute);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);
        return c;
    }

}
